﻿<%@ WebHandler Language="C#" Class="BBSHandler" %>

using System;
using System.Web;
using System.Collections.Generic;

using EHSubmit.Logic;
using EHSubmit.Model;
using EHSubmit.Util;


/// <summary>
/// 与BBS有关的处理类
/// </summary>
public class BBSHandler : BaseAjaxHandler
{
    protected override void Mapping()
    {
        OprationMap["AnnPage"] = "AnnPage";
        OprationMap["add"] = "AddAnn";
        OprationMap["addRes"] = "AddRes";
        OprationMap["ViewPage"] = "ViewPage";
        OprationMap["List_Institute"] = "FindInstitutes";            //获取学院信息
        OprationMap["List_Depts"] = "FindDepartmentsOfInstitute";    //查找相关学院的专业信息
    }

    public void AnnPage()
    {
        if (Validate(ERole.Student) || Validate(ERole.Teacher))
        {
            AnnouncesManager announcesManager = new AnnouncesManager();
            int start = Convert.ToInt32(Context.Request["start"]);
            int limit = Convert.ToInt32(Context.Request["limit"]);

            int pageIndex = start / limit + 1;
            IList<EHSubmit.Model.Announces> AnnouncesList = null;
            int totalCount = 0;
            AnnouncesList = announcesManager.GetAnnounces(pageIndex);
            totalCount = announcesManager.GetAnnouncesPageCount();
            string jsonData = "{}";
            if (AnnouncesList.Count != 0)
            {
                jsonData = JsonHelper.Serialize(AnnouncesList);
            }

            IsSuccess = true;

            ResponseText = "{success:true,totalProperty:" + totalCount.ToString() + ",Ann:" + jsonData + "}";

        }
        else
        {
            Message = "您没有权限执行此操作";
        }
    }

    public void AddAnn()
    {
        if (Validate(ERole.Student) || Validate(ERole.Teacher))
        {
            string Ann = Context.Request["Ann"].ToString();
            //string departmentName = Context.Request["AddAnn_department"].ToString();
            AnnouncesManager announceManager = new AnnouncesManager();
            //MID自增,SendTime.RecvTime要处理，IsViewed、IsSDeleted，IsSDeleted默认不处理
            EHSubmit.Model.Announces announce = JsonHelper.Deserialize<EHSubmit.Model.Announces>(Ann);
            announce.Body = announce.Body == null ? "" : announce.Body;
            announce.SubmitTime = DateTime.Now;
            announce.resLastTime = DateTime.Now;
            announce.rescnt = 0;
            //announce.DepartmentName = departmentName;

            announceManager.AddAnnounces(announce);
            IsSuccess = true;
            ResponseText = "{success:true,msg:\"发送成功\"}";      //必须返回json数据格式 ，success需要确定
        }
        else
        {
            Message = "您没有权限执行此操作";
        }
    }


    public void ViewPage()
    {

        if (Validate(ERole.Student) || Validate(ERole.Teacher))
        {
            RestoresManager restoresManager = new RestoresManager();
            int start = Convert.ToInt32(Context.Request["start"]);
            int limit = Convert.ToInt32(Context.Request["limit"]);
            int AnnID = Convert.ToInt32(Context.Request["AnnID"]);
            int pageIndex = start / limit + 1;
            IList<EHSubmit.Model.Restores> RestoresList = null;
            int totalCount = 0;
            RestoresList = restoresManager.GetRestores(pageIndex,AnnID);
            totalCount = restoresManager.GetRestoresPageCount(AnnID);
            string jsonData = "{}";
            if (RestoresList.Count != 0)
            {
                jsonData = JsonHelper.Serialize(RestoresList);
            }

            IsSuccess = true;

            ResponseText = "{success:true,totalProperty:" + totalCount.ToString() + ",root:" + jsonData + "}";

        }
        else
        {
            Message = "您没有权限执行此操作";
        }
    }

    public void FindInstitutes()
    {

        if (Validate(ERole.Student) || Validate(ERole.Teacher))
        {
            InstituteManager istManager = new InstituteManager();
            IList<Institute> istList = new List<Institute>();
            string istJson = null;

            istList = istManager.GetInstitutes();
            if (istList.Count != 0)
            {
                istJson = JsonHelper.Serialize(istList);

                IsSuccess = true;
                ResponseText = "{" + string.Format("institutes:{0}", istJson) + "}";
            }
            else
            {
                Message = "无学院信息!";
            }
        }
        else
        {
            Message = "您没有权限执行此操作";
        }
    }



    public void FindDepartmentsOfInstitute()
    {
        if (Validate(ERole.Student) || Validate(ERole.Teacher))
        {
            DepartmentManager deptManager = new DepartmentManager();
            IList<Department> deptList = new List<Department>();
            string deptJson = null;

            //---学院id
            int insID = WebHelper.IntSafeConvert(Context.Request["insID"], -1);
            if (insID != -1)
            {
                deptList = deptManager.GetDepartmentByInstituteID(insID);
                if (deptList.Count != 0)
                {
                    deptJson = JsonHelper.Serialize(deptList);

                    IsSuccess = true;
                    ResponseText = "{" + string.Format("total:{0},depts:{1}", deptList.Count, deptJson) + "}";
                }
                else
                {
                    IsSuccess = true;
                    ResponseText = "{" + string.Format("total:{0},depts:[]", deptList.Count) + "}";
                }
            }
            else
            {
                Message = Constants.INFO_UNKNOWN_ERR;
            }
        }
        else
        {
            Message = Constants.INFO_NO_ACCESS;
        }
    }


    public void AddRes()
    {
        try
        {
            string Res = Context.Request["Res"].ToString();

            RestoresManager restoreManager = new RestoresManager();
            
            EHSubmit.Model.Restores restore = JsonHelper.Deserialize<EHSubmit.Model.Restores>(Res);
            restore.ResTime = DateTime.Now;
            restore.RestoreBody = restore.RestoreBody == null ? "" : restore.RestoreBody;
            if (Validate(ERole.Student))
            {
                restore.Role = "学生";
            }
            if (Validate(ERole.Teacher))
            {
                restore.Role = "教师";
            }
            restoreManager.AddRestores(restore);
            restoreManager.UpdateAnn(restore.ResTime, restore.ResAnnID);
            IsSuccess = true;
            ResponseText = "{success:true,msg:\"发送成功\"}";      //必须返回json数据格式 ，success需要确定
        }
        
        catch
        {
            Message = "您没有权限执行此操作";
        }
    }
    
}
